package com.ruoyi.web.controller.business;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.dto.PlatformsDto;
import com.ruoyi.common.core.domain.entity.Platforms;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.shiro.util.AuthorizationUtils;
import com.ruoyi.system.service.IPlatformService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *  平台管理
 *
 * @author zz
 */
@Controller
@RequestMapping("/system/platform")
public class PlatformController extends BaseController
{
    private String prefix = "business/platform";

    @Autowired
    private IPlatformService platformService;

    @RequiresPermissions("system:platform:view")
    @GetMapping()
    public String platform()
    {
        return prefix + "/platform";
    }

    @RequiresPermissions("system:platform:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(PlatformsDto platformsDto)
    {
        startPage();
        List<Platforms> list = platformService.selectAll(platformsDto);
        return getDataTable(list);
    }

    @Log(title = "平台管理", businessType = BusinessType.EXPORT)
    @RequiresPermissions("system:platform:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(PlatformsDto platformsDto)
    {
        List<Platforms> list = platformService.selectAll(platformsDto);
        ExcelUtil<Platforms> util = new ExcelUtil<Platforms>(Platforms.class);
        return util.exportExcel(list, "平台数据");
    }

    /**
     * 新增平台
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存平台
     */
    @RequiresPermissions("system:platform:add")
    @Log(title = "平台管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated Platforms platform)
    {
        AuthorizationUtils.clearAllCachedAuthorizationInfo();
        return toAjax(platformService.insert(platform));

    }

    /**
     * 修改平台
     */
    @RequiresPermissions("system:platform:edit")
    @GetMapping("/edit/{platformId}")
    public String edit(@PathVariable("platformId") Long platformId, ModelMap mmap)
    {
        mmap.put("platform", platformService.selectByPrimaryKey(platformId));
        return prefix + "/edit";
    }

    /**
     * 修改保存平台
     */
    @RequiresPermissions("system:platform:edit")
    @Log(title = "平台管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated Platforms platform)
    {
        AuthorizationUtils.clearAllCachedAuthorizationInfo();
        return toAjax(platformService.updateByPrimaryKey(platform));
    }


    @RequiresPermissions("system:platform:remove")
    @Log(title = "平台管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        platformService.deleteByPrimaryKey(ids);
        return success();
    }

}